package cn.sirun.com.fca.account.view.impl;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;

import com.jungly.gridpasswordview.GridPasswordView;
import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.SRPUtils;
import com.tencent.cloud.uikit.utils.DialogUtils;
import com.tencent.cloud.uikit.utils.SystemUiUtils;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.constant.AccountIntentConst;
import cn.sirun.com.fca.account.constant.CaptchaConst;
import cn.sirun.com.fca.account.model.bind.PinSetFirstRequest;
import cn.sirun.com.fca.account.model.bind.PinSetFirstResponse;
import cn.sirun.com.fca.account.model.bind.PinSetParamResponse;
import cn.sirun.com.fca.account.model.bind.PinSetParamsRequest;
import cn.sirun.com.fca.account.presenter.ISettingNewPinPresent;
import cn.sirun.com.fca.account.presenter.impl.SettingNewPinPresenter;
import cn.sirun.com.fca.account.view.ISettingInitalPinView;
import cn.sirun.com.fca.account.view.ISettingNewPinView;

public class SettingNewPinFlowView extends BaseLoadingFlowView<ISettingNewPinPresent>
        implements ISettingInitalPinView {
    private GridPasswordView mPinGPV;
    private TextView mTitleTV, mSureTV, mPromptTV;

    private String mFrontPinString;
    private String pin = "";

    public SettingNewPinFlowView(Activity activity) {
        super(activity);
    }

    public SettingNewPinFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        SystemUiUtils.showActionBar(activity, "");
    }

    @Override
    protected ISettingNewPinPresent createPresenter() {
        return new SettingNewPinPresenter();
    }

    private void initView() {
        setContentView(R.layout.setting_new_pin_activity);
        final Activity activity = getActivity();
        mPinGPV = activity.findViewById(R.id.gpv_pin);
        mSureTV = activity.findViewById(R.id.tv_next);
        mTitleTV = activity.findViewById(R.id.tv_pin_title);
        mPromptTV = activity.findViewById(R.id.tv_prompt);

        mTitleTV.setText(ResourcesUtils.getString(R.string.input_new_pin2));
        mSureTV.setText(ResourcesUtils.getString(R.string.title_sure_commit));
        mPinGPV.togglePasswordVisibility();

        mPinGPV.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {
                if (psw.length() == CaptchaConst.PIN_LENGTH) {
                    if (psw.equals(mFrontPinString)) {
                        mSureTV.setBackgroundResource(R.drawable.s_btn_bg_border);
                        mSureTV.setEnabled(true);
                        mPromptTV.setVisibility(View.INVISIBLE);
                    } else {
                        mPromptTV.setVisibility(View.VISIBLE);
                    }
                } else {
                    mSureTV.setBackgroundResource(R.drawable.s_btn_default_bg_border);
                    mSureTV.setEnabled(false);
                }
            }

            @Override
            public void onInputFinish(String psw) {
                pin = psw;
            }
        });

        mSureTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PinSetParamsRequest request = new PinSetParamsRequest();
                getPresenter().getPinSetParams(request);
            }
        });
        initData();
    }

    private void initData() {
        mFrontPinString = getActivity().getIntent()
                .getStringExtra(AccountIntentConst.SETTING_PIN);
    }

    @Override
    public void getPinSetParams(BaseResponse<PinSetParamResponse> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            /**
             * 获取到PIN设置信息，再请求设置PIN接口
             */
            PinSetFirstRequest request = new PinSetFirstRequest();
            if(response.getData().getSalt() != null && response.getData().getGroup() != null){
                String salt = response.getData().getSalt();
                String group = response.getData().getGroup();
                request.setPin(SRPUtils.encryptData(pin, salt, group));
            }

            getPresenter().pinReset(request);
        }
    }

    @Override
    public void pinSetFirst(BaseResponse<PinSetFirstResponse> result) {

    }

    @Override
    public void pinResetResult(BaseResponse<PinSetFirstResponse> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            DialogUtils.showToast(getActivity(), R.string.pin_update_pass);
        }
    }
}
